package com.xtravel.common.helper;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.ByteArrayOutputStream;

public class ImageHelper {

    /*
    * 缩放原图,如果maxWidth/maxHeight缩放比例不是2的次幂，会造成缩略图比预期大的情况
     */
    public static Bitmap getThumbBitmapBySampleSize(String imgUrl, int maxWidth, int maxHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        BitmapFactory.decodeFile(imgUrl, options);
        options.inSampleSize = getScaleOfBitmap(options, maxWidth, maxHeight);
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        return BitmapFactory.decodeFile(imgUrl, options);
    }

    /*
  * 缩放原图,返回缩略图大小和预期一样
   */
    public static Bitmap getThumbBitmapByMatrix(String imgUrl, int maxWidth,
                                                int maxHeight) {
        Bitmap bm = getThumbBitmapBySampleSize(imgUrl, maxWidth, maxHeight);
        int width = bm.getWidth();
        int height = bm.getHeight();
        if ((width > maxWidth && maxWidth > 0) || (height > maxHeight && maxHeight > 0)) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            float scale = getMatrixOfBitmap(width, height, maxWidth,
                    maxHeight);
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            Bitmap matrixBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
            bm.recycle();
            return matrixBitmap;
        }
        return bm;
    }

    public static Bitmap scaleBitmap(Bitmap bitmap, int maxWidth,
                                     int maxHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scale = getMatrixOfBitmap(width, height, maxWidth,
                maxHeight);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private static int getScaleOfBitmap(BitmapFactory.Options options,
                                        int maxWidth, int maxHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (maxWidth <= 0) {
            maxWidth = width;
        }
        if (maxHeight <= 0) {
            maxHeight = height;
        }
        if (height > maxHeight || width > maxWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > maxHeight
                    && (halfWidth / inSampleSize) > maxWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private static float getMatrixOfBitmap(int width, int height,
                                           int maxWidth, int maxHeight) {
        if (height > maxHeight || width > maxWidth) {
            float scaleWidth = ((float) maxWidth) / width;
            float scaleHeight = ((float) maxHeight) / height;
            if (maxWidth <= 0 && maxHeight <= 0) {
                return 1;
            } else if (maxWidth <= 0) {
                return scaleHeight;
            } else if (maxHeight <= 0) {
                return scaleWidth;
            } else
                return scaleWidth > scaleHeight ? scaleHeight : scaleWidth;
        }
        return 1;
    }

    public static Bitmap CompressBitmap(Bitmap bitmap,
                                        int quality) {
        if (bitmap != null) {
            ByteArrayOutputStream sm = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, quality, sm);
            byte[] bytes = sm.toByteArray();
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        } else {
            return null;
        }
    }

    public static byte[] ConvertBitmapToBytes(Bitmap bitmap) {
        ByteArrayOutputStream sm = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 100, sm);
        return sm.toByteArray();
    }
}
